package com.site.wisdom_construction.controller;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.iotModule.*;
import com.site.wisdom_construction.entity.qualityModule.QualityDangerRecord;
import com.site.wisdom_construction.entity.safetyModule.SafetyDangerRecord;
import com.site.wisdom_construction.entity.safetyModule.SafetyInspection;
import com.site.wisdom_construction.service.iotModule.*;
import com.site.wisdom_construction.service.qualityModule.QualityDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyInspectionService;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author daiyifei @Date 2022/6/15 @Description
 */
@Api(tags = "测试数据推送")
@RestController
@RequestMapping("TestPush_D")
@CrossOrigin
public class PushDataController_D {
  @Autowired private AiMonitoringService aiMonitoringService;

  @Autowired private RealTimePersonService realTimePersonService;

  @Autowired private SafetyInspectionService safetyInspectionService;

  @Autowired private SafetyDangerRecordService safetyDangerRecordService;

  @Autowired private QualityDangerRecordService qualityDangerRecordService;

  @Autowired private TowerCraneRealtimeService towerCraneRealtimeService;

  @Autowired private TowerCraneWarningService towerCraneWarningService;

  @Autowired private HighSupportsService highSupportsService;

  @Autowired private HighSupportsRecordService highSupportsRecordService;


  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "巡检点数据上传")
  @PostMapping(value = "/InspectionPoint")
  @NoToken
  public void InspectionPoint(@RequestBody Map map) {
    map.put("projectId",1);
    List<SafetyInspection> safetyInspectionList = safetyInspectionService.selectSafetyInspection(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectionPoint";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String uuid = "6aeb4ec810dc4609b12c1c51f7d33861";
    for (int i = 0;i<safetyInspectionList.size();i++){
      jsonObject.put("inspectionId","3203011D610501"+uuid);
      jsonObject.put("site",safetyInspectionList.get(i).getInspectionName());
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "巡检内容数据上传")
  @PostMapping(value = "/InspectionPointContent")
  @NoToken
  public void InspectionPointContent(@RequestBody Map map) {
    map.put("projectId",1);
    List<SafetyDangerRecord> safetyDangerRecordList = safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectionPointContent";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String uuid = "6aeb4ec810dc4609b12c1c51f7d33861";
    for (int i = 0;i<safetyDangerRecordList.size();i++){
      jsonObject.put("inspectionId","3203011D610501"+uuid);
      jsonObject.put("inspectionContentId",uuid);
      String inspectionTime = String.valueOf(safetyDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("inspectionTime",inspectionTime);
      jsonObject.put("checkPerson",safetyDangerRecordList.get(i).getManagerialName());
      jsonObject.put("checkContent",safetyDangerRecordList.get(i).getSafetyDescription());
      jsonObject.put("rectifyPerson",safetyDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate",inspectionTime);
      String[] strings = {"1"};
      strings[0]=safetyDangerRecordList.get(i).getSafetyImg();
      jsonObject.put("urls",strings);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "巡检整改回复")
  @PostMapping(value = "/RectifyInspectionPointContent")
  @NoToken
  public void RectifyInspectionPointContent(@RequestBody Map map) {
    map.put("projectId",1);
    List<SafetyDangerRecord> safetyDangerRecordList = safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyInspectionPointContent";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String uuid = "6aeb4ec810dc4609b12c1c51f7d33861";
    for (int i = 0;i<safetyDangerRecordList.size();i++){
      jsonObject.put("inspectionId","3203011D610501"+uuid);
      jsonObject.put("inspectionContentId",uuid);
      String finalRectifyDate = String.valueOf(safetyDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate",finalRectifyDate);
      jsonObject.put("rectifyContents",safetyDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0]=safetyDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls",strings);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
      System.out.println("请求接口次数:"+i);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "隐患随手拍数据上传")
  @PostMapping(value = "/shootContent")
  @NoToken
  public void shootContent(@RequestBody Map map) {
    map.put("projectId",1);
    List<SafetyDangerRecord> safetyDangerRecordList = safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/shootContent";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String uuid = "6aeb4ec810dc4609b12c1c51f7d33861";
    for (int i = 0;i<safetyDangerRecordList.size();i++){
      jsonObject.put("shootId",uuid);
      String inspectionTime = String.valueOf(safetyDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("shootTime",inspectionTime);
      jsonObject.put("shootPerson",safetyDangerRecordList.get(i).getManagerialName());
      jsonObject.put("shootAdress",safetyDangerRecordList.get(i).getSafetyInspectionName());
      jsonObject.put("shotContent",safetyDangerRecordList.get(i).getSafetyDescription());
      jsonObject.put("rectifyPerson",safetyDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate",inspectionTime);
      String[] strings = {"1"};
      strings[0]=safetyDangerRecordList.get(i).getSafetyImg();
      jsonObject.put("urls",strings);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "随手拍整改回复内容上传")
  @PostMapping(value = "/RectifyShootContent")
  @NoToken
  public void RectifyShootContent(@RequestBody Map map) {
    map.put("projectId",1);
    List<SafetyDangerRecord> safetyDangerRecordList = safetyDangerRecordService.selectSafetyDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyShootContent";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String uuid = "6aeb4ec810dc4609b12c1c51f7d33861";
    for (int i = 0;i<safetyDangerRecordList.size();i++){
      jsonObject.put("shootId",uuid);
      String finalRectifyDate = String.valueOf(safetyDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate",finalRectifyDate);
      jsonObject.put("rectifyContents",safetyDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0]=safetyDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls",strings);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
      System.out.println("请求接口次数:"+i);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "检查单数据上传")
  @PostMapping(value = "/InspectContentInfo")
  @NoToken
  public void InspectContentInfo(@RequestBody Map map) {
    map.put("projectId",1);
    List<QualityDangerRecord> qualityDangerRecordList = qualityDangerRecordService.selectQualityDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/InspectContentInfo";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String times = String.valueOf(TimeUtils.getStampH());
    jsonObject.put("checkNumber","3203011D610501"+1667268646);
    jsonObject.put("checkNumType",1);
    jsonObject.put("idcard","220183199004185232");
    jsonObject.put("checkPerson","潘旭");
    jsonObject.put("isRectify",1);
    for (int i = 0;i<qualityDangerRecordList.size();i++){
      String finalRectifyDate = String.valueOf(qualityDangerRecordList.get(i).getDiscoverTime()) + " 00:00:00";
      jsonObject.put("checkDate",finalRectifyDate);
      jsonObject.put("checkContent",qualityDangerRecordList.get(i).getQualityDescription());
      String[] strings = {"1"};
      strings[0]=qualityDangerRecordList.get(i).getQualityImg();
      jsonObject.put("urls",strings);
      jsonObject.put("rectifyPerson",qualityDangerRecordList.get(i).getWorkersName());
      jsonObject.put("rectifyDate",finalRectifyDate);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
      System.out.println("请求接口次数:"+i);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "检查单整改完成数据上传")
  @PostMapping(value = "/RectifyContentInfo")
  @NoToken
  public void RectifyContentInfo(@RequestBody Map map) {
    map.put("projectId",1);
    List<QualityDangerRecord> qualityDangerRecordList = qualityDangerRecordService.selectQualityDangerRecord(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1022/api/RectifyContentInfo";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String times = String.valueOf(TimeUtils.getStampH());
    jsonObject.put("checkNumber","3203011D610501"+1667268646);
    for (int i = 0;i<qualityDangerRecordList.size();i++){
      String finalRectifyDate = String.valueOf(qualityDangerRecordList.get(i).getAbarbeitungTime()) + " 00:00:00";
      jsonObject.put("finalRectifyDate",finalRectifyDate);
      jsonObject.put("rectifyContents",qualityDangerRecordList.get(i).getAbarbeitungDescription());
      String[] strings = {"1"};
      strings[0]=qualityDangerRecordList.get(i).getAbarbeitungImg();
      jsonObject.put("rectifyUrls",strings);
      System.out.println("jsonObject:"+jsonObject);
      JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
      System.out.println("jsonObjectL:"+jsonObjectL);
      System.out.println("请求接口次数:"+i);
    }
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "塔机实时数据上传")
  @PostMapping(value = "/UploadCraneHistory")
  @NoToken
  public void UploadCraneHistory(@RequestBody Map map) throws ParseException {
    map.put("deviceId","9270009");
    TowerCraneRealtime towerCraneRealtime = towerCraneRealtimeService.findTowerCraneRealtimeByDeviceId(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1011/deviceupload/api/UploadCraneHistory";
    String moniterTime = String.valueOf(towerCraneRealtime.getJkDate());
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    jsonObject.put("moniterTime",getTime(moniterTime));
    jsonObject.put("deviceId",towerCraneRealtime.getDeviceId());
    jsonObject.put("load",Math.toIntExact(Math.round(towerCraneRealtime.getDz())));
    jsonObject.put("range",Math.toIntExact(Math.round(towerCraneRealtime.getFd())));
    jsonObject.put("moment",Math.toIntExact(Math.round(towerCraneRealtime.getLjb())));
    jsonObject.put("rotation",Math.toIntExact(Math.round(towerCraneRealtime.getHz())));
    jsonObject.put("height",Math.toIntExact(Math.round(towerCraneRealtime.getHeight())));
    jsonObject.put("windSpeed",Math.toIntExact(Math.round(towerCraneRealtime.getWindspeed())));
    jsonObject.put("dip",Math.toIntExact(Math.round(towerCraneRealtime.getQj())));
    jsonObject.put("multiplyingPower",Math.toIntExact(Math.round(towerCraneRealtime.getBl())));
    System.out.println("jsonObject:"+jsonObject);
    JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
    System.out.println("jsonObjectL:"+jsonObjectL);
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "塔机预警数据上传")
  @PostMapping(value = "/CraneAlarmInfo")
  @NoToken
  public void CraneAlarmInfo(@RequestBody Map map) throws ParseException {
    map.put("deviceId","010601387");
    TowerCraneWarning towerCraneWarning = towerCraneWarningService.findTowerCraneWarningByDeviceId(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1011/deviceupload/api/CraneAlarmInfo";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String happenTime = String.valueOf(towerCraneWarning.getJkDate());
    jsonObject.put("deviceId",towerCraneWarning.getDeviceId());
    jsonObject.put("warnExplain","一般报警");
    jsonObject.put("warnLevel","一般预警");
    jsonObject.put("warnContent",towerCraneWarning.getAlarm());
    jsonObject.put("idcard", "320323199203192839");
    jsonObject.put("happenTime", getTime(happenTime));
    System.out.println("jsonObject:"+jsonObject);
    JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
    System.out.println("jsonObjectL:"+jsonObjectL);
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "高支模实时数据上传")
  @PostMapping(value = "/HighFormworkHistory")
  @NoToken
  public void HighFormworkHistory(@RequestBody Map map) throws ParseException {
    map.put("projectId",1);
    HighSupports highSupports = highSupportsService.findHighSupportsByNew(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1011/deviceupload/api/HighFormworkHistory";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String collectionTime = String.valueOf(highSupports.getSendTime());
    System.out.println("collectionTime:"+collectionTime);
    jsonObject.put("collectionTime", getTime(collectionTime));
    jsonObject.put("deviceId",highSupports.getEquipmentProjectId());
    jsonObject.put("temperature",highSupports.getTemperature());
    jsonObject.put("horizontalAngle",highSupports.getSlantX());
    jsonObject.put("coordinate",highSupports.getSlantY());
    jsonObject.put("translation", highSupports.getDisplacement());
    System.out.println("jsonObject:"+jsonObject);
    JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
    System.out.println("jsonObjectL:"+jsonObjectL);
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "高支模预警数据上传")
  @PostMapping(value = "/HighFormworkAlarmInfo")
  @NoToken
  public void HighFormworkAlarmInfo(@RequestBody Map map) throws ParseException {
    map.put("projectId",1);
    HighSupportsRecord highSupportsRecord = highSupportsRecordService.findHighSupportsRecordByNew(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1011/deviceupload/api/HighFormworkAlarmInfo";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String happenTime = String.valueOf(highSupportsRecord.getSendTime());
    jsonObject.put("happenTime", getTime(happenTime));
    jsonObject.put("deviceId",highSupportsRecord.getEquipmentId());
    jsonObject.put("warnExplain",highSupportsRecord.getMeasurePoint());
    jsonObject.put("warnValue",highSupportsRecord.getAlarmValue());
    jsonObject.put("warnContent",highSupportsRecord.getMeasurePoint()+"超高预警");
    System.out.println("jsonObject:"+jsonObject);
    JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
    System.out.println("jsonObjectL:"+jsonObjectL);
  }

  /** 智慧工地对接智慧安监平台-大桥局 */
  @ApiOperation(value = "深基坑实时数据上传")
  @PostMapping(value = "/DeepPitHistory")
  @NoToken
  public void DeepPitHistory(@RequestBody Map map) {
    map.put("projectId",1);
    HighSupportsRecord highSupportsRecord = highSupportsRecordService.findHighSupportsRecordByNew(map);
    HttpRequest httpRequest = new HttpRequest();
    JSONObject jsonObject = new JSONObject();
    String path = "http://221.229.205.168:1011/deviceupload/api/DeepPitHistory";
    jsonObject.put("belongedTo","AJ320301-1");
    jsonObject.put("recordNumber","3203011D610501");
    String happenTime = String.valueOf(highSupportsRecord.getSendTime());
    jsonObject.put("happenTime", happenTime);
    jsonObject.put("deviceId",highSupportsRecord.getEquipmentId());
    jsonObject.put("warnExplain",highSupportsRecord.getMeasurePoint());
    jsonObject.put("warnValue",highSupportsRecord.getAlarmValue());
    jsonObject.put("warnContent",highSupportsRecord.getMeasurePoint()+"超高预警");
    System.out.println("jsonObject:"+jsonObject);
    JSONObject jsonObjectL = httpRequest.pushJsonData(path,jsonObject);
    System.out.println("jsonObjectL:"+jsonObjectL);
  }

  public String getTime(String datea) throws ParseException {
    SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sim1.parse(datea);
    return sim2.format(date);
  }
}
